package com.mcxx.modules.miniSalvation.order.service.write;

import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.family.dto.SalvationObjectDTO;
import com.mcxx.modules.miniSalvation.family.param.SalvationObjectParam;
import com.mcxx.modules.miniSalvation.order.dao.read.BusiInsureDetailReadDao;
import com.mcxx.modules.miniSalvation.order.dao.read.OrderCaregiversReadDao;
import com.mcxx.modules.miniSalvation.order.dao.read.OrderReadDao;
import com.mcxx.modules.miniSalvation.order.dao.write.BusiInsureDetailWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.ExInsuranceDetailsDTO;
import com.mcxx.modules.miniSalvation.order.dto.ExInsuranceDrawUpDTO;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.core.utils.UUIDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
@TaTransactional
public class BusiInsureDetailWriteService  {

    @Resource
    private BusiInsureDetailWriteDao exInsuranceDrawUpWriteDao;


    @Resource
    private BusiInsureDetailReadDao exInsuranceDrawUpReadDao;

    @Resource
    private OrderCaregiversReadDao orderCaregiversReadDao;

    @Resource
    private OrderReadDao orderReadDao;



    public int addInsurance(ExInsuranceDrawUpDTO insurance, UserAccountVo accountVo){
        insurance.setId(UUIDUtils.getUUID());
        insurance.setSignDate(new Date());
        insurance.setSignOrgId(accountVo.getAreaCode());
        insurance.setOrgLevel(accountVo.getAreaLevel());
        insurance.setInsureMembers(0);
        insurance.setInsureStatus("01");

        insurance.setConfirmDate(new Date());
        insurance.setCreateDate(new Date());
        insurance.setUpdateDate(new Date());
       return exInsuranceDrawUpWriteDao.addInsurance(insurance);
    }


    public int addInsuranceDetails(String insuranceId,String flag, String objIds,String areaCode){
        int restFlag = 0;
        ExInsuranceDetailsDTO detailsDTO = null;
        List<ExInsuranceDetailsDTO> insuranceDetails = new ArrayList<>();
        String [] array = objIds.split(",");
        List<String> items = Arrays.asList(array);
        List<SalvationObjectDTO> objItems = orderReadDao.getSalvationObjectListByIds(items,new Date(),new Date());
        for (SalvationObjectDTO objectDTO : objItems) {
            detailsDTO = new ExInsuranceDetailsDTO();
            detailsDTO.setId(UUIDUtils.getUUID());
            detailsDTO.setFamilyId(objectDTO.getFamilyId());
            detailsDTO.setInsureId(insuranceId);
            detailsDTO.setMemberId(objectDTO.getMemberId());
            detailsDTO.setAreaCode(objectDTO.getAreaCode());
            detailsDTO.setMemberName(objectDTO.getName());
            detailsDTO.setIdCard(objectDTO.getIdcard());
            detailsDTO.setPhone(flag);
            detailsDTO.setProvertyType(objectDTO.getStandType());
            detailsDTO.setSelfcareLevel(objectDTO.getSelfcareLevel() == null?"01":objectDTO.getSelfcareLevel());
            detailsDTO.setSupplyType(objectDTO.getSupplyType());
            detailsDTO.setSupplyOrgId(objectDTO.getSupplyOrgId());
            detailsDTO.setInsureQuote("0");
            detailsDTO.setSalaryNumber(objectDTO.getSalaryNumber());
            detailsDTO.setCreateDate(new Date());
            detailsDTO.setUpdateDate(new Date());
            detailsDTO.setRemark(areaCode);

            insuranceDetails.add(detailsDTO);
        }


        restFlag = exInsuranceDrawUpWriteDao.addInsuranceDetails(insuranceDetails);
        if(restFlag > 0){
            if(insuranceId != "1"){
                exInsuranceDrawUpWriteDao.updateInsuranceObjNumber(insuranceId,items.size());
            }
        }
        return restFlag;
    }


    public int updateInsuraceDetails(String insuranceId,String objIds){
        int restFlag = 0;
        String [] array = objIds.split(",");
        List<String> items = Arrays.asList(array);
        restFlag = exInsuranceDrawUpWriteDao.updateInsuraceDetails(insuranceId,items);
        if(restFlag > 0){
            exInsuranceDrawUpWriteDao.updateInsuranceObjNumber(insuranceId,items.size());
        }
       return restFlag;

    }


    public int confirm(String insuranceId,String userId){


        int restFlag = 0;
        ExInsuranceDrawUpDTO insuranceInfo = exInsuranceDrawUpReadDao.getInsuranceInfo(insuranceId);
        Date date = new Date();
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date time_date = null;
        try {
            time_date = time.parse(time.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        ExInsuranceDrawUpDTO draw = new ExInsuranceDrawUpDTO();
        draw.setUpdateDate(new Date());
        draw.setConfirmId(userId);


        if(time_date.compareTo(insuranceInfo.getStartDate()) == -1){
            restFlag = exInsuranceDrawUpWriteDao.confirm(insuranceId,"03",draw);
        }else if(time_date.compareTo(insuranceInfo.getStartDate()) == 1 && time_date.compareTo(insuranceInfo.getEndDate()) == -1){
            restFlag =  exInsuranceDrawUpWriteDao.confirm(insuranceId,"04",draw);
        }else if(time_date.compareTo(insuranceInfo.getStartDate()) == 1 && time_date.compareTo(insuranceInfo.getEndDate()) == 1){
            restFlag = exInsuranceDrawUpWriteDao.confirm(insuranceId,"05",draw);
        }

        return restFlag;
    }


    public int del(String insuranceId){
        int restFlag = 0;
      restFlag = exInsuranceDrawUpWriteDao.delInsurace(insuranceId);
      restFlag = restFlag +  exInsuranceDrawUpWriteDao.delInsuranceDetails(insuranceId);
        return restFlag;
    }


    public int updateInsurance(ExInsuranceDrawUpDTO exInsuranceDrawUpDTO){
        exInsuranceDrawUpDTO.setUpdateDate(new Date());
       return exInsuranceDrawUpWriteDao.updateInsurance(exInsuranceDrawUpDTO);
    }


    public int delSelectObj(String objIds){
        String [] array = objIds.split(",");
        List<String> items = Arrays.asList(array);
       return exInsuranceDrawUpWriteDao.delSelectObj(items);
    }



    public Page<SalvationObjectDTO> getSupplyObjectListNotAdded(SalvationObjectParam param, PageParam pageParam, UserAccountVo accountVo) {

        QueryParamUtil.setBaseParamValue(param, accountVo);
        if (CommonUtil.invalidArgs(param.getSendMonth())) {
            param.setSendMonth(DateUtil.getMonthString());
        }


        if(!param.getExportType().equals("1")){
            System.err.println("来了");
            orderReadDao.beginPager(pageParam);
        }
        param.setStartTime(new Date());
        param.setEndTime(new Date());
        List<SalvationObjectDTO> list = orderReadDao.getSupplyObjectListNotAdded(param);

        if (param.getExportType().equals("1")) {
            ExInsuranceDetailsDTO detailsDTO = null;
            List<ExInsuranceDetailsDTO> insuranceDetails = new ArrayList<>();

            for (SalvationObjectDTO objectDTO : list) {
                detailsDTO = new ExInsuranceDetailsDTO();
                detailsDTO.setId(UUIDUtils.getUUID());
                detailsDTO.setFamilyId(objectDTO.getFamilyId());
                detailsDTO.setInsureId(param.getInsuranceId());
                detailsDTO.setMemberId(objectDTO.getMemberId());
                detailsDTO.setAreaCode(objectDTO.getAreaCode());
                detailsDTO.setMemberName(objectDTO.getMasterName() == null? "默认" : objectDTO.getMasterName());
                detailsDTO.setIdCard(objectDTO.getIdcard());
                detailsDTO.setProvertyType(objectDTO.getStandType());
                detailsDTO.setSelfcareLevel(objectDTO.getSelfcareLevel() == null ? "01" : objectDTO.getSelfcareLevel());
                detailsDTO.setSupplyType(objectDTO.getSupplyType());
                detailsDTO.setSupplyOrgId(objectDTO.getSupplyOrgId());
                detailsDTO.setInsureQuote("0");
                detailsDTO.setSalaryNumber(objectDTO.getSalaryNumber());
                detailsDTO.setCreateDate(new Date());
                detailsDTO.setUpdateDate(new Date());
                detailsDTO.setRemark(accountVo.getAreaCode());

                insuranceDetails.add(detailsDTO);
            }



            int restFlag = exInsuranceDrawUpWriteDao.addInsuranceDetails(insuranceDetails);
            if (restFlag > 0) {
                exInsuranceDrawUpWriteDao.updateInsuranceObjNumber(param.getInsuranceId(), list.size());
            }
            return null;
        }


        return orderReadDao.endPager(list);
    }
}
